//
//  UIButton+JwCate.m
//  JwPack
//
//  Created by 陈警卫 on 2022/8/20.
//

#import "UIButton+JwCate.h"
#import <objc/runtime.h>

@implementation UIButton (JwCate)

static char jw_buttonAction_key;

/// MARK: 添加点击事件
- (void)jw_addTargetAction:(didJwButtonActionBlock)block {
    [self jw_addTargetAction:block forControlEvents:(UIControlEventTouchUpInside)];
}

/// MARK: 添加点击事件
- (void)jw_addTargetAction:(didJwButtonActionBlock)block forControlEvents:(UIControlEvents)controlEvents {
    objc_setAssociatedObject(self, &jw_buttonAction_key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(jw_buttonAction:) forControlEvents:controlEvents];
}

/// MARK: 事件响应
- (void)jw_buttonAction:(UIButton *)button {
    didJwButtonActionBlock blockAction = (didJwButtonActionBlock)objc_getAssociatedObject(self, &jw_buttonAction_key);
    if (blockAction) {
        blockAction(self);
    }
}

@end
